package com.webproject.action.RateManager;

import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.rate.MonthRate;
import com.webproject.entity.rate.WaterElectricityRate;
import com.webproject.service.RateManager.RateManagerService;

public class RateManagerAction extends BaseAction{

	private RateManagerService rateManagerService;
	private Page mpage;
	private boolean success;
	private String msg;
	private InputStream inputStream;
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Page getMpage() {
		return mpage;
	}

	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}

	public void setRateManagerService(RateManagerService rateManagerService) {
		this.rateManagerService = rateManagerService;
	}
	
	public String queryWaterEle(){
		mpage=getPage();
		rateManagerService.queryWaterEle(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String editWaterEle(){
		WaterElectricityRate rate=getWaterEle();
		boolean b=rateManagerService.canSaveWaterEle(rate, getOper());
		if(!b){
			success=false;
			msg="当月数据已存在";
		}else{
			success=true;
			msg="";
			if(getOper().equals("add")){
				rateManagerService.saveWaterEle(rate);
			}else if (getOper().equals("edit")){
				rateManagerService.updateWaterEle(rate);
			}
		}
		return SUCCESS;
	}
	private WaterElectricityRate getWaterEle(){
		WaterElectricityRate rate=new WaterElectricityRate();
		String id =getRequest().getParameter("id");
		String roomId=getRequest().getParameter("roomId");
		String overTime=getRequest().getParameter("overTime");
		String bMonthElectric=getRequest().getParameter("bMonthElectric");
		String eMonthElectric=getRequest().getParameter("eMonthElectric");
		String bMonthWater=getRequest().getParameter("bMonthWater");
		String eMonthWater=getRequest().getParameter("eMonthWater");
		rate.setId(id);
		rate.setRoomId(roomId);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			rate.setOverTime(sdf.parse(overTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rate.setbMonthElectric(new Float(bMonthElectric));
		rate.seteMonthElectric(new Float(eMonthElectric));
		rate.setbMonthWater(new Float(bMonthWater));
		rate.seteMonthWater(new Float(eMonthWater));
		return rate;
	}
	
	public String queryMonthRate(){
		mpage=getPage();
		rateManagerService.queryMonthRate(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	
	public String editMonthRate(){
		MonthRate rate=getMonthRate();
		if(getOper().equals("add")){
			rateManagerService.saveMonthRate(rate);
		}else if (getOper().equals("edit")){
			rateManagerService.updateMonthRate(rate);
		}
		return SUCCESS;
	}
	
	private MonthRate getMonthRate(){
		MonthRate rate=new MonthRate();
		String id=getRequest().getParameter("id");
		String rentState=getRequest().getParameter("rentState");
		String warmRentPrice=getRequest().getParameter("warmRentPrice");
		String electricPrice=getRequest().getParameter("electricPrice");
		String waterPrice=getRequest().getParameter("waterPrice");
		String overTime=getRequest().getParameter("overTime");
		String roomId=getRequest().getParameter("roomId");
		rate.setId(id);
		rate.setRentState(Integer.parseInt(rentState));
		rate.setWarmRentPrice(new Float(warmRentPrice));
		rate.setElectricPrice(new Float(electricPrice));
		rate.setWaterPrice(new Float(waterPrice));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			rate.setOverTime(sdf.parse(overTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rate.setRoomId(roomId);
		return rate;
	}
	
	public String exportExcel(){
		log.debug("--------exportExcel------------");
		return SUCCESS;
	}
}
